package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvCity,tvWeather,tvTemp,tvWind,tvPm;
    private ImageView ivIcon;
    private List<WeatherInfo> infoList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoList = JsonParse.getInstance().getInfosFromJson(MainActivity.this);
        initView();
        getCityData("北京");
    }
    private void initView(){
        tvCity=findViewById(R.id.tvCity);
        tvWeather=findViewById(R.id.tvWeather);
        tvTemp=findViewById(R.id.tvTemp);
        tvWind=findViewById(R.id.tvWind);
        tvPm=findViewById(R.id.tvPm);
        ivIcon=findViewById(R.id.imageView);
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
    }
    private void setData(WeatherInfo info){
        if(info==null)return;
        tvCity.setText(info.getCity());
        tvWeather.setText(info.getWeather());
        tvTemp.setText(info.getTemp());
        tvWind.setText(info.getWind());
        tvPm.setText(info.getPm());
        if(("晴转多云").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.cloud_sun);
        }else if(("多云").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.clouds);
        }else if(("晴").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.sun);
        }
    }
    private  void getCityData(String city){
        for(WeatherInfo info : infoList){
            if(info.getCity().equals(city)){
                setData(info);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case  R.id.button:
                getCityData("上海");
                break;
            case  R.id.button2:
                getCityData("北京");
                break;
            case  R.id.button3:
                getCityData("广州");
                break;
        }
    }
}